package com.yckj.activechicken.business.common.service.impl;

import com.yckj.activechicken.business.common.mapper.ApproverInfoMapper;
import com.yckj.activechicken.business.common.model.ApproverInfo;
import com.yckj.activechicken.business.common.model.ApproverInfoExample;
import com.yckj.activechicken.business.common.model.vo.ApproverInfoVo;
import com.yckj.activechicken.business.common.service.IApproverService;
import com.yckj.activechicken.common.utils.StringUtils;
import com.yckj.activechicken.common.utils.security.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * Title:com.yckj.activechicken.business.common.service.impl
 * </p>
 * <p/>
 * <p>
 * Description: 描述【审批人管理业务处理实体】
 * </p>
 * <p/>
 * <p>
 * Copyright: Copyright (c) 2019
 * </p>
 * <p/>
 * <p>
 * Company: 英才汇硕信息技术有限公司
 * </p>
 *
 * @author zhoujy
 * @version 1.0
 * @created 2020/6/15 15:25
 */
@Service
public class ApproverServiceImpl implements IApproverService {
    @Autowired
    private ApproverInfoMapper approverInfoMapper;

    /**
     * 设置流程审批人
     *
     * @param approverInfoVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addApproverList(ApproverInfoVo approverInfoVo) {
        String username = SecurityUtils.getUsername();
        Date newDate = new Date();
        String id = approverInfoVo.getId();
        String deploymentId = approverInfoVo.getDeploymentId();

        ApproverInfoExample example = new ApproverInfoExample();
        example.createCriteria().andProcessDefinitionIdEqualTo(id);
        approverInfoMapper.deleteByExample(example);

        List<ApproverInfo> approverInfoList = approverInfoVo.getApproverInfoList();
        if (StringUtils.isNotEmpty(approverInfoList)) {
            for (ApproverInfo approverInfo : approverInfoList) {
                approverInfo.setProcessDefinitionId(id);
                approverInfo.setProcessDefinitionKey(approverInfoVo.getKey());
                approverInfo.setDeploymentId(deploymentId);
                approverInfo.setCreateBy(username);
                approverInfo.setCreateTime(newDate);
                approverInfo.setUpdateBy(username);
                approverInfo.setUpdateTime(newDate);
                approverInfoMapper.insertSelective(approverInfo);
            }
        }

    }

    /**
     * 查询流程环节信息
     *
     * @param taskId
     * @param processDefinitionId
     * @return
     */
    @Override
    public ApproverInfo queryApproverByTaskId(String taskId, String processDefinitionId) {
        ApproverInfoExample example = new ApproverInfoExample();
        example.createCriteria().andProcessDefinitionIdEqualTo(processDefinitionId)
                .andTaskIdEqualTo(taskId);
        List<ApproverInfo> approverInfoList = approverInfoMapper.selectByExample(example);
        if (StringUtils.isNotEmpty(approverInfoList)) {
            return approverInfoList.get(0);
        } else {
            return null;
        }
    }
}
